package com.dailywork.basepro.thread;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 线程安全
 *
 * @author think
 * @version 1.0.0
 * @since 2020/4/26
 */
public class ThreadSafe1 {
    public static void main(String[] args) {
        //Text-->Date
        String dateStr = "2018-06-20 11:25:56";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        System.out.println(date.toString());

        //Date-->Text
        date = new Date();
        instant = date.toInstant();
        zone = ZoneId.systemDefault();
        localDateTime = LocalDateTime.ofInstant(instant, zone);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowStr = localDateTime.format(format);
        System.out.println(nowStr);

        //LocalDateTime获取毫秒
        System.out.println(localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());

        localDateTime = LocalDateTime.now();
        String dateString = localDateTime.format(dateTimeFormatter);
        System.out.println(dateString);

        instant = Instant.now();
        System.out.println(instant.plusMillis(TimeUnit.MILLISECONDS.toMillis(500)));
    }
}
